<!-- build time:Tue Jul 21 2020 12:41:22 GMT+0800 (GMT+08:00) --><!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=2"><meta name="theme-color" content="#222"><meta name="generator" content="Hexo 4.2.0"><link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png"><link rel="icon" type="image/png" sizes="32x32" href="/img/blog_favicon/favicon-32x32-next.png"><link rel="icon" type="image/png" sizes="16x16" href="/img/blog_favicon/favicon-16x16-next.png"><link rel="mask-icon" href="/images/logo.svg" color="#222"><meta name="baidu-site-verification" content="r15J5zOS2KI43yBS"><link rel="stylesheet" href="/css/main.css"><link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"><link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css"><link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/themes/blue/pace-theme-minimal.min.css"><script src="//cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/pace.min.js"></script><script id="hexo-configurations">var NexT=window.NexT||{},CONFIG={hostname:new URL("http://www.zhengjiaao.cn").hostname,root:"/",scheme:"Muse",version:"7.6.0",exturl:!1,sidebar:{position:"left",display:"post",padding:18,offset:12,onmobile:!1},copycode:{enable:!0,show_result:!0,style:null},back2top:{enable:!0,sidebar:!1,scrollpercent:!0},bookmark:{enable:!0,color:"#222",save:"auto"},fancybox:!0,mediumzoom:!1,lazyload:!1,pangu:!1,comments:{style:"tabs",active:"valine",storage:!0,lazyload:!1,nav:null,activeClass:"valine"},algolia:{appID:"",apiKey:"",indexName:"",hits:{per_page:10},labels:{input_placeholder:"Search for Posts",hits_empty:"We didn't find any results for the search: ${query}",hits_stats:"${hits} results found in ${time} ms"}},localsearch:{enable:!0,trigger:"auto",top_n_per_article:1,unescape:!1,preload:!1},path:"search.xml",motion:{enable:!0,async:!1,transition:{post_block:"fadeIn",post_header:"slideDownIn",post_body:"slideDownIn",coll_header:"slideLeftIn",sidebar:"slideUpIn"}}}</script><meta property="og:type" content="article"><meta property="og:title" content="Springboot-Solr-配置和使用"><meta property="og:url" content="http://www.zhengjiaao.cn/posts/2656162415.html"><meta property="og:site_name" content="宇宙小神特别萌"><meta property="og:locale" content="zh_CN"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-80b835a9bb1fb199.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-079bca222cdbd1fd.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-993b68c341435466.png"><meta property="article:published_time" content="2020-01-03T08:14:51.000Z"><meta property="article:modified_time" content="2020-07-20T10:37:55.320Z"><meta property="article:author" content="宇宙小神特别萌"><meta property="article:tag" content="Solr"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/15645795-80b835a9bb1fb199.png"><link rel="canonical" href="http://www.zhengjiaao.cn/posts/2656162415.html"><script id="page-configurations">CONFIG.page={sidebar:"",isHome:!1,isPost:!0}</script><title>Springboot-Solr-配置和使用 | 宇宙小神特别萌</title><noscript><style>.sidebar-inner,.use-motion .brand,.use-motion .collection-header,.use-motion .comments,.use-motion .menu-item,.use-motion .pagination,.use-motion .post-block,.use-motion .post-body,.use-motion .post-header{opacity:initial}.use-motion .site-subtitle,.use-motion .site-title{opacity:initial;top:initial}.use-motion .logo-line-before i{left:initial}.use-motion .logo-line-after i{right:initial}</style></noscript><link rel="alternate" href="/atom.xml" title="宇宙小神特别萌" type="application/atom+xml"></head><body itemscope itemtype="http://schema.org/WebPage"><div class="container use-motion"><div class="headband"></div><header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="header-inner"><div class="site-brand-container"><div class="site-meta custom-logo"><div class="site-meta-headline"><a><img class="custom-logo-image" src="/img/blog_favicon/favicon-32x32-next.png" alt="宇宙小神特别萌"></a></div><div><a href="/" class="brand" rel="start"><span class="logo-line-before"><i></i></span> <span class="site-title">宇宙小神特别萌</span> <span class="logo-line-after"><i></i></span></a></div><p class="site-subtitle">ZhengJa</p></div><div class="site-nav-toggle"><div class="toggle" aria-label="切换导航栏"><span class="toggle-line toggle-line-first"></span> <span class="toggle-line toggle-line-middle"></span> <span class="toggle-line toggle-line-last"></span></div></div></div><nav class="site-nav"><ul id="menu" class="menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签<span class="badge">27</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档<span class="badge">116</span></a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="fa fa-fw fa-sitemap"></i>站点地图</a></li><li class="menu-item menu-item-messageboard"><a href="/messageboard/" rel="section"><i class="fa fa-fw fa-address-book"></i>留言板</a></li><li class="menu-item menu-item-links"><a href="/links/" rel="section"><i class="fa fa-fw fa-user-plus"></i>友链</a></li><li class="menu-item menu-item-shuoshuo"><a href="/shuoshuo/" rel="section"><i class="fa fa-fw fa-file-text-o"></i>说说</a></li><li class="menu-item menu-item-search"><a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索</a></li></ul></nav><div class="site-search"><div class="popup search-popup"><div class="search-header"><span class="search-icon"><i class="fa fa-search"></i></span><div class="search-input-container"><input autocomplete="off" autocorrect="off" autocapitalize="none" placeholder="搜索..." spellcheck="false" type="text" id="search-input"></div><span class="popup-btn-close"><i class="fa fa-times-circle"></i></span></div><div id="search-result"></div></div><div class="search-pop-overlay"></div></div></div></header><div class="back-to-top"><i class="fa fa-arrow-up"></i> <span>0%</span></div><div class="reading-progress-bar"></div><a role="button" class="book-mark-link book-mark-link-fixed"></a> <a href="https://github.com/zhengjiaao" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin:130px 106px" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><main class="main"><div class="main-inner"><div class="content-wrap"><div class="content"><div class="posts-expand"><article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN"><link itemprop="mainEntityOfPage" href="http://www.zhengjiaao.cn/posts/2656162415.html"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="image" content="/my-images/avatar.gif"><meta itemprop="name" content="宇宙小神特别萌"><meta itemprop="description" content="人生就是一次幸运的旅行！"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="宇宙小神特别萌"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">Springboot-Solr-配置和使用</h1><div class="post-meta"><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-calendar-o"></i> </span><span class="post-meta-item-text">发表于</span> <time title="创建时间：2020-01-03 16:14:51" itemprop="dateCreated datePublished" datetime="2020-01-03T16:14:51+08:00">2020-01-03</time> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-calendar-check-o"></i> </span><span class="post-meta-item-text">更新于</span> <time title="修改时间：2020-07-20 18:37:55" itemprop="dateModified" datetime="2020-07-20T18:37:55+08:00">2020-07-20</time> </span><span id="/posts/2656162415.html" class="post-meta-item leancloud_visitors" data-flag-title="Springboot-Solr-配置和使用" title="热度"><span class="post-meta-item-icon"><i class="fa fa-eye"></i> </span><span class="post-meta-item-text">热度：</span> <span class="leancloud-visitors-count"></span> <span>℃</span> </span><span class="post-meta-item" title="热度" id="busuanzi_container_page_pv" style="display:none"><span class="post-meta-item-icon"><i class="fa fa-eye"></i> </span><span class="post-meta-item-text">热度：</span> <span id="busuanzi_value_page_pv"></span> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-comment-o"></i> </span><span class="post-meta-item-text">Valine：</span> <a title="valine" href="/posts/2656162415.html#comments" itemprop="discussionUrl"><span class="post-comments-count valine-comment-count" data-xid="/posts/2656162415.html" itemprop="commentCount"></span></a></span><br><span class="post-meta-item" title="本文字数"><span class="post-meta-item-icon"><i class="fa fa-file-word-o"></i> </span><span class="post-meta-item-text">本文字数：</span> <span>34k</span> </span><span class="post-meta-item" title="阅读时长"><span class="post-meta-item-icon"><i class="fa fa-clock-o"></i> </span><span class="post-meta-item-text">阅读时长 &asymp;</span> <span>31 分钟</span></span></div></header><div class="post-body" itemprop="articleBody"><meta name="referrer" content="no-referrer"><a id="more"></a><p><strong>目录</strong> <img src="https://upload-images.jianshu.io/upload_images/15645795-80b835a9bb1fb199.png" alt="Springboot Solr 配置和使用目录.png"></p><h2 id="一、springboot-solr简单测试"><a href="#一、springboot-solr简单测试" class="headerlink" title="一、springboot+solr简单测试"></a>一、springboot+solr简单测试</h2><h3 id="1、创建springboot项目和测试代码"><a href="#1、创建springboot项目和测试代码" class="headerlink" title="1、创建springboot项目和测试代码"></a>1、创建springboot项目和测试代码</h3><p>环境：springboot+solr+swagger2<br>说明：在项目此处连接的是solr中core库 ，方式二创建的<br>pom.xml依赖</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-data-solr<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure><p>具体pom文件内容：</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-test<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">scope</span>&gt;</span>test<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-tomcat<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">scope</span>&gt;</span>provided<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!--集成swagger-API--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>io.swagger<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>swagger-models<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.5.21<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--&lt;scope&gt;compile&lt;/scope&gt;--&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>io.swagger<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>swagger-annotations<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.5.21<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--&lt;scope&gt;compile&lt;/scope&gt;--&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>io.springfox<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>springfox-swagger-ui<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.9.2<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>io.springfox<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>springfox-swagger2<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.9.2<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">exclusions</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>io.swagger<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>swagger-models<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>io.swagger<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>swagger-annotations<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">exclusions</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure><p>.yml文件</p><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">server:</span></span><br><span class="line">  <span class="attr">context-path:</span> <span class="string">/</span></span><br><span class="line">  <span class="attr">port:</span> <span class="number">8080</span></span><br><span class="line"><span class="attr">spring:</span></span><br><span class="line">  <span class="attr">data:</span></span><br><span class="line">    <span class="attr">solr:</span></span><br><span class="line">      <span class="comment">#后面这个core就是文件夹的名称 ，这里也可以不用写这个 ，如果这里不指定core ，那么在代码中使用的时候 ，就需要指定core。</span></span><br><span class="line">      <span class="comment">#host:  http://127.0.0.1:8983/solr/core</span></span><br><span class="line">      <span class="comment">#代码中可以指定core的地方有注释可以看</span></span><br><span class="line">      <span class="attr">host:</span>  <span class="string">http://127.0.0.1:8983/solr</span></span><br></pre></td></tr></table></figure><p>添加swagger2配置<br>创建文件夹：config\Swagger2Config.java</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.dist.config;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.google.common.base.Predicate;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Bean;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;</span><br><span class="line"><span class="keyword">import</span> springfox.documentation.builders.ApiInfoBuilder;</span><br><span class="line"><span class="keyword">import</span> springfox.documentation.builders.PathSelectors;</span><br><span class="line"><span class="keyword">import</span> springfox.documentation.service.ApiInfo;</span><br><span class="line"><span class="keyword">import</span> springfox.documentation.service.Contact;</span><br><span class="line"><span class="keyword">import</span> springfox.documentation.spi.DocumentationType;</span><br><span class="line"><span class="keyword">import</span> springfox.documentation.spring.web.plugins.Docket;</span><br><span class="line"><span class="keyword">import</span> springfox.documentation.swagger2.annotations.EnableSwagger2;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> yangmin</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2018/8/15</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@desc</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="meta">@EnableSwagger</span>2</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Swagger2Config</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Docket <span class="title">adminApi</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Docket(DocumentationType.SWAGGER_2)</span><br><span class="line">                .groupName(<span class="string">"Admin API"</span>)</span><br><span class="line">                .forCodeGeneration(<span class="keyword">true</span>)</span><br><span class="line">                .pathMapping(<span class="string">"/"</span>)</span><br><span class="line">                .select()</span><br><span class="line">                .paths(paths())</span><br><span class="line">                .build()</span><br><span class="line">                .apiInfo(apiInfo())</span><br><span class="line">                .useDefaultResponseMessages(<span class="keyword">false</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> Predicate&lt;String&gt; <span class="title">paths</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> PathSelectors.regex(<span class="string">"^/(?!error).*$"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> ApiInfo <span class="title">apiInfo</span><span class="params">()</span></span>&#123;</span><br><span class="line">        Contact contact = <span class="keyword">new</span> Contact(<span class="string">"BaiDu"</span>, <span class="string">"controller://baidu.com"</span>, <span class="string">" zhengja@dist.com.cn"</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> ApiInfoBuilder()</span><br><span class="line">                .title(<span class="string">"个人SpringBoot测试系统"</span>)</span><br><span class="line">                .description(<span class="string">"开发API文档"</span>)</span><br><span class="line">                .contact(contact)</span><br><span class="line">                .version(<span class="string">"1.0"</span>)</span><br><span class="line">                .build();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在resources/static下创建index.html<br>把以下内容复制 ，需要更改端口和项目地址</p><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Title<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>进入首页<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--更改成自己的端口号--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">" http://localhost:8080/swagger-ui.html"</span>&gt;</span>进入swagger页面测试2<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure><p>controller下SolrController.java类</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.dist.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> io.swagger.annotations.Api;</span><br><span class="line"><span class="keyword">import</span> io.swagger.annotations.ApiOperation;</span><br><span class="line"><span class="keyword">import</span> io.swagger.annotations.ApiParam;</span><br><span class="line"><span class="keyword">import</span> org.apache.solr.client.solrj.SolrClient;</span><br><span class="line"><span class="keyword">import</span> org.apache.solr.client.solrj.SolrQuery;</span><br><span class="line"><span class="keyword">import</span> org.apache.solr.client.solrj.SolrServerException;</span><br><span class="line"><span class="keyword">import</span> org.apache.solr.client.solrj.response.QueryResponse;</span><br><span class="line"><span class="keyword">import</span> org.apache.solr.client.solrj.response.UpdateResponse;</span><br><span class="line"><span class="keyword">import</span> org.apache.solr.common.SolrDocument;</span><br><span class="line"><span class="keyword">import</span> org.apache.solr.common.SolrDocumentList;</span><br><span class="line"><span class="keyword">import</span> org.apache.solr.common.SolrInputDocument;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMethod;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestParam;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RestController;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhengja@dist.com.cn</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@data</span> 2019/8/15 15:43</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(value = <span class="string">"rest/solr"</span>)</span><br><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="meta">@Api</span>(tags = &#123;<span class="string">"SolrController"</span>&#125;,description = <span class="string">"Solr 全文检索"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SolrController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> SolrClient client;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"添加文档内容"</span>,notes = <span class="string">"向文档中添加域 ，必须有id域 ，域的名称必须在scheme.xml中定义"</span>,httpMethod = <span class="string">"GET"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"insert"</span>,method = RequestMethod.GET)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">validator</span><span class="params">(@ApiParam(value = <span class="string">"name"</span>)</span> @RequestParam String name,</span></span><br><span class="line"><span class="function">                            @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"age"</span>)</span> @RequestParam String age)</span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            String idStr = String.valueOf(System.currentTimeMillis());</span><br><span class="line"></span><br><span class="line">            SolrInputDocument document = <span class="keyword">new</span> SolrInputDocument();</span><br><span class="line">            document.setField(<span class="string">"id"</span>, idStr);</span><br><span class="line">            document.setField(<span class="string">"name"</span>, name);</span><br><span class="line">            document.setField(<span class="string">"age"</span>,age);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 把文档对象写入索引库</span></span><br><span class="line">            client.add(<span class="string">"core"</span>,document);<span class="comment">//如果配置文件中没有指定core ，这个方法的第一个参数就需要指定core名称,比如client.add("core", doc);</span></span><br><span class="line">            client.commit(<span class="string">"core"</span>);<span class="comment">//如果配置文件中没有指定core ，这个方法的第一个参数就需要指定core名称client.commit("core");</span></span><br><span class="line">            <span class="keyword">return</span> idStr;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"error"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"更新文档内容"</span>,notes = <span class="string">"更新文档内容 ，跟添加的区别是：id不能变 ，其他的可以变"</span>,httpMethod = <span class="string">"GET"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"updateDocument"</span>,method = RequestMethod.GET)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">updateDocument</span><span class="params">(@ApiParam(value = <span class="string">"idStr"</span>)</span> @RequestParam String idStr,</span></span><br><span class="line"><span class="function">                               @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"name"</span>)</span> @RequestParam String name,</span></span><br><span class="line"><span class="function">                               @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"age"</span>)</span> @RequestParam String age) <span class="keyword">throws</span> Exception</span>&#123;</span><br><span class="line">        <span class="comment">// 创建一个文档对象, 向文档中添加域 ，必须有id域 ，域的名称必须在scheme.xml中定义</span></span><br><span class="line">        SolrInputDocument document = <span class="keyword">new</span> SolrInputDocument();</span><br><span class="line">        document.setField(<span class="string">"id"</span>, idStr);</span><br><span class="line">        document.setField(<span class="string">"name"</span>, name);</span><br><span class="line">        document.setField(<span class="string">"age"</span>,age);</span><br><span class="line">        <span class="comment">// 把文档对象写入索引库</span></span><br><span class="line">        client.add(<span class="string">"core"</span>,document);</span><br><span class="line">        <span class="comment">// 提交</span></span><br><span class="line">        client.commit(<span class="string">"core"</span>);</span><br><span class="line">        <span class="keyword">return</span> document;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"全量或增量更新-数据库"</span>,notes = <span class="string">"java操作Solr的全量或增量更新 ，可以结合定时任务做定时全量或增量更新"</span>,httpMethod = <span class="string">"PUT"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"updateSolrData"</span>,method = RequestMethod.PUT)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">updateSolrData</span><span class="params">()</span> <span class="keyword">throws</span> SolrServerException, IOException </span>&#123;</span><br><span class="line">        <span class="comment">//创建一个查询对象</span></span><br><span class="line">        SolrQuery solrQuery = <span class="keyword">new</span> SolrQuery();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//增量更新全部完成；注意这里entity的值为solr-data-config.xml里entity标签里的name值</span></span><br><span class="line">        <span class="keyword">final</span> String SOLR_DELTA_PARAM = <span class="string">"/dataimport?command=delta-import&amp;entity=order_info&amp;clean=false&amp;commit=true"</span>;</span><br><span class="line">        <span class="comment">//全量更新全部完成</span></span><br><span class="line">        <span class="keyword">final</span> String SOLR_FULL_PARAM = <span class="string">"/dataimport?command=full-import&amp;entity=order_info&amp;clean=true&amp;commit=true"</span>;</span><br><span class="line">        <span class="comment">//设置更新方式</span></span><br><span class="line">        solrQuery.setRequestHandler(SOLR_DELTA_PARAM);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 执行查询</span></span><br><span class="line">        QueryResponse query = client.query(<span class="string">"core"</span>,solrQuery);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//提交</span></span><br><span class="line">        client.commit(<span class="string">"core"</span>);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"查询文档内容"</span>,notes = <span class="string">"查询文档内容"</span>,httpMethod = <span class="string">"GET"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"queryDocument"</span>,method = RequestMethod.GET)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">queryDocument</span><span class="params">(@ApiParam(value = <span class="string">"条件"</span>,defaultValue = <span class="string">"*:*"</span>)</span> @RequestParam String condition,</span></span><br><span class="line"><span class="function">                              @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"连接文件夹 默 core"</span>,defaultValue = <span class="string">"core"</span>)</span> @RequestParam String collection,</span></span><br><span class="line"><span class="function">                              @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"分页起始 默 1"</span>,defaultValue = <span class="string">"1"</span>)</span> @RequestParam Integer pageStart,</span></span><br><span class="line"><span class="function">                              @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"分页结束 默 10"</span>,defaultValue = <span class="string">"10"</span>)</span> @RequestParam Integer pageEnd) <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="comment">// 创建一个查询条件</span></span><br><span class="line">        SolrQuery solrQuery = <span class="keyword">new</span> SolrQuery();</span><br><span class="line">        <span class="comment">// 设置查询条件</span></span><br><span class="line">        solrQuery.setQuery(condition);</span><br><span class="line">        <span class="comment">// 设置分页</span></span><br><span class="line">        solrQuery.setStart(pageStart);</span><br><span class="line">        solrQuery.setRows(pageEnd);</span><br><span class="line">        <span class="comment">// 执行查询</span></span><br><span class="line">        QueryResponse query = client.query(collection,solrQuery);</span><br><span class="line">        <span class="comment">// 取查询结果</span></span><br><span class="line">        SolrDocumentList solrDocumentList = query.getResults();</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">"总记录数："</span> + solrDocumentList.getNumFound());</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (SolrDocument sd : solrDocumentList) &#123;</span><br><span class="line">            System.out.println(sd.get(<span class="string">"id"</span>));</span><br><span class="line">            System.out.println(sd.get(<span class="string">"name"</span>));</span><br><span class="line">            System.out.println(sd.get(<span class="string">"age"</span>));</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> solrDocumentList;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"删除文档"</span>,notes = <span class="string">"删除文档"</span>,httpMethod = <span class="string">"DELETE"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"deteleDocument"</span>,method = RequestMethod.DELETE)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">deteleDocument</span><span class="params">(@ApiParam(value = <span class="string">"连接文件夹 默 core"</span> ,defaultValue = <span class="string">"core"</span>)</span> @RequestParam String collection,</span></span><br><span class="line"><span class="function">                               @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"idStr"</span>)</span> @RequestParam String idStr) <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="comment">// 根据id删除</span></span><br><span class="line">        UpdateResponse response = client.deleteById(collection, idStr);</span><br><span class="line">        <span class="comment">// 根据条件删除</span></span><br><span class="line">        <span class="comment">// httpSolrServer.deleteByQuery("");</span></span><br><span class="line">        <span class="comment">// 提交</span></span><br><span class="line">        client.commit(collection);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> response;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>然后启动该项目之后 ，进入项目地址：<br><a href="http://localhost:8080/swagger-ui.html" target="_blank" rel="noopener">http://localhost:8080/swagger-ui.html</a> 执行-即可测试插入索引操作。<br>插入成功之后 ，访问：<br><a href="http://127.0.0.1:8983/solr/#/" target="_blank" rel="noopener">http://127.0.0.1:8983/solr/#/</a></p><p>在solr管理界面的左下方有一个下拉框 ，选择你创建的core之后 ，再选择Query ，往下拖到最后 ，点击Execute Query即可看见你刚才插入的索引。<br><img src="https://upload-images.jianshu.io/upload_images/15645795-079bca222cdbd1fd.png" alt="solr效果.png"></p><h5 id="其他问题："><a href="#其他问题：" class="headerlink" title="其他问题："></a>其他问题：</h5><p>　　刚才我们代码中插入了两个列(id、name) ，如果你想新增一个列 ，比如名称叫做age ，然后启动项目 ，在插入索引 ，在solr管理平台中就看不见这个列名和列对应的值 ，原因是id、name这两列在solr中已经默认配置了。这种情况解决方式如下：<br>　　在你创建的core文件夹下面 ，打开conf文件夹 ，修改里面的managed-schema文件 ，修改地点如下：</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 132行后面增加(后面这几行随意位置增加就可以)</span></span><br><span class="line">&lt;field name=<span class="string">"age"</span> type=<span class="string">"text_general"</span> indexed=<span class="string">"true"</span> stored=<span class="string">"true"</span>/&gt;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 245行后面增加(后面这几行随意位置增加就可以)</span></span><br><span class="line">&lt;copyField source=<span class="string">"age"</span> dest=<span class="string">"text"</span>/&gt;</span><br></pre></td></tr></table></figure><p>修改完成之后重启solr ，然后重新插入索引就可以成功操作了。</p><p>再测试：<br>SolrController.java</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">SolrInputDocument doc = <span class="keyword">new</span> SolrInputDocument();</span><br><span class="line">            doc.setField(<span class="string">"id"</span>, idStr);  <span class="comment">//id是默认配置 ，在你创建的core文件夹下面 ，打开conf文件夹 ，修改里面的managed-schema文件</span></span><br><span class="line">            doc.setField(<span class="string">"name"</span>, content); <span class="comment">// name是默认配置</span></span><br><span class="line">            doc.setField(<span class="string">"age"</span>,<span class="string">"23"</span>); <span class="comment">//自定义配置：在你创建的core文件夹下面 ，打开conf文件夹 ，修改里面的managed-schema文件 ，添加列 age</span></span><br></pre></td></tr></table></figure><p>效果图：<br><img src="https://upload-images.jianshu.io/upload_images/15645795-993b68c341435466.png" alt="solr效果.png"></p><h2 id="二、升级版测试"><a href="#二、升级版测试" class="headerlink" title="二、升级版测试"></a>二、升级版测试</h2><p>添加配置：<br>pom.xml</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--solr 全文检索--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-data-solr<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--自定义工具类：解决属性值null指针异常 ，将 null 转成 ""--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.json<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>json<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">version</span>&gt;</span>20160810<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>net.sf.json-lib<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>json-lib<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.4<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">classifier</span>&gt;</span>jdk15<span class="tag">&lt;/<span class="name">classifier</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.alibaba<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>fastjson<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.2.4<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure><p>*.yml文件配置 solr</p><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring:</span></span><br><span class="line">  <span class="attr">data:</span></span><br><span class="line">    <span class="attr">solr:</span></span><br><span class="line">      <span class="comment">#后面这个core就是文件夹的名称 ，这里也可以不用写这个 ，如果这里不指定core ，那么在代码中使用的时候 ，就需要指定core。</span></span><br><span class="line">      <span class="comment">#host:  http://127.0.0.1:8983/solr/core</span></span><br><span class="line">      <span class="comment">#代码中可以指定core的地方有注释可以看</span></span><br><span class="line">      <span class="attr">host:</span>  <span class="string">http://127.0.0.1:8983/solr</span></span><br></pre></td></tr></table></figure><p>PropertyNullToStrUtil.java<br>解决：对象属性值为 null的值转为 空字符串”” 的工具类</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.dist.util;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> net.sf.json.JSONNull;</span><br><span class="line"><span class="keyword">import</span> org.json.JSONArray;</span><br><span class="line"><span class="keyword">import</span> org.json.JSONObject;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Iterator;</span><br><span class="line"></span><br><span class="line"><span class="comment">/** 对象属性值为 null的值转为 空字符串""</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhengja@dist.com.cn</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@data</span> 2019/8/20 13:59</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PropertyNullToStrUtil</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 将json对象中包含的null和JSONNull属性修改成""</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> jsonObj</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> JSONObject <span class="title">filterNull</span><span class="params">(JSONObject jsonObj)</span> </span>&#123;</span><br><span class="line">        Iterator&lt;String&gt; it = jsonObj.keys();</span><br><span class="line">        Object obj = <span class="keyword">null</span>;</span><br><span class="line">        String key = <span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">while</span> (it.hasNext()) &#123;</span><br><span class="line">            key = it.next();</span><br><span class="line">            obj = jsonObj.get(key);</span><br><span class="line">            <span class="keyword">if</span> (obj <span class="keyword">instanceof</span> JSONObject) &#123;</span><br><span class="line">                filterNull((JSONObject) obj);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (obj <span class="keyword">instanceof</span> JSONArray) &#123;</span><br><span class="line">                JSONArray objArr = (JSONArray) obj;</span><br><span class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; objArr.length(); i++) &#123;</span><br><span class="line">                    filterNull(objArr.getJSONObject(i));</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (obj == <span class="keyword">null</span> || obj <span class="keyword">instanceof</span> JSONNull) &#123;</span><br><span class="line">                jsonObj.put(key, <span class="string">""</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (obj.equals(<span class="keyword">null</span>)) &#123;</span><br><span class="line">                jsonObj.put(key, <span class="string">""</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> jsonObj;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>UserEntityDto.java @Field(）对应core字段</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.dist.entity;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> io.swagger.annotations.ApiModel;</span><br><span class="line"><span class="keyword">import</span> io.swagger.annotations.ApiModelProperty;</span><br><span class="line"><span class="keyword">import</span> lombok.Data;</span><br><span class="line"><span class="keyword">import</span> org.apache.solr.client.solrj.beans.Field;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.Serializable;</span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"></span><br><span class="line"><span class="comment">/** class 类中的字段上 ，solr的core中没有使用 <span class="doctag">@Field</span>,那么该部分字段值 ，不会存储到solr数据库中</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@program</span>: springbootdemo</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span>: 2018/12/26 10:03</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span>: Mr.Zheng</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@ApiModel</span>(value = <span class="string">"测试dto"</span>)</span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserEntityDto</span> <span class="keyword">implements</span> <span class="title">Serializable</span> </span>&#123;</span><br><span class="line">    <span class="meta">@ApiModelProperty</span>(value = <span class="string">"id"</span>,required = <span class="keyword">true</span>)</span><br><span class="line">    <span class="meta">@Field</span>(<span class="string">"id"</span>)</span><br><span class="line">    <span class="keyword">private</span> Long id;</span><br><span class="line">    <span class="meta">@ApiModelProperty</span>(value = <span class="string">"guid"</span>)</span><br><span class="line">    <span class="keyword">private</span> String guid;</span><br><span class="line">    <span class="meta">@ApiModelProperty</span>(value = <span class="string">"name"</span>)</span><br><span class="line">    <span class="meta">@Field</span>(<span class="string">"name"</span>)</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="meta">@ApiModelProperty</span>(value = <span class="string">"age"</span>)</span><br><span class="line">    <span class="meta">@Field</span>(<span class="string">"age"</span>)</span><br><span class="line">    <span class="keyword">private</span> String age;</span><br><span class="line">    <span class="meta">@ApiModelProperty</span>(value = <span class="string">"createTime"</span>)</span><br><span class="line">    <span class="keyword">private</span> Date createTime;</span><br><span class="line">    <span class="meta">@ApiModelProperty</span>(value = <span class="string">"lastUpdateTime"</span>)</span><br><span class="line">    <span class="keyword">private</span> Date lastUpdateTime;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>UserEntity.java @Field(）对应corename字段</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.dist.entity;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> lombok.Data;</span><br><span class="line"><span class="keyword">import</span> org.apache.solr.client.solrj.beans.Field;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Component;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.persistence.*;</span><br><span class="line"><span class="keyword">import</span> java.io.Serializable;</span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@program</span>: springbootdemo</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span>: 2018/12/26 10:03</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span>: Mr.Zheng</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Entity</span>(name = <span class="string">"UserEntity"</span>)</span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="meta">@Table</span>(name=<span class="string">"t_user"</span>)</span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserEntity</span> <span class="keyword">implements</span> <span class="title">Serializable</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Id</span></span><br><span class="line">    <span class="meta">@GeneratedValue</span></span><br><span class="line">    <span class="meta">@Column</span>(name = <span class="string">"ID"</span>,nullable = <span class="keyword">true</span>)</span><br><span class="line">    <span class="meta">@Field</span>(<span class="string">"id"</span>)</span><br><span class="line">    <span class="keyword">private</span> Long id;</span><br><span class="line">    <span class="meta">@Column</span>(name=<span class="string">"GUID"</span>)</span><br><span class="line">    <span class="keyword">private</span> String guid;</span><br><span class="line">    <span class="meta">@Column</span>(name = <span class="string">"NAME"</span>,nullable = <span class="keyword">true</span>,length = <span class="number">50</span>)</span><br><span class="line">    <span class="meta">@Field</span>(<span class="string">"user_name"</span>)</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="meta">@Column</span>(name = <span class="string">"AGE"</span>,nullable = <span class="keyword">true</span>,length = <span class="number">50</span>)</span><br><span class="line">    <span class="meta">@Field</span>(<span class="string">"user_age"</span>)</span><br><span class="line">    <span class="keyword">private</span> String age;</span><br><span class="line">    <span class="meta">@Column</span>(name = <span class="string">"CREATETIME"</span>,nullable = <span class="keyword">true</span>)</span><br><span class="line">    <span class="keyword">private</span> Date createTime;</span><br><span class="line">    <span class="meta">@Column</span>(name = <span class="string">"LASTUPDATETIME"</span>,nullable = <span class="keyword">true</span>)</span><br><span class="line">    <span class="meta">@Field</span>(<span class="string">"user_lastUpdateTime"</span>)</span><br><span class="line">    <span class="keyword">private</span> Date lastUpdateTime;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"UserEntity&#123;"</span> +</span><br><span class="line">                <span class="string">"id="</span> + id +</span><br><span class="line">                <span class="string">", guid='"</span> + guid + <span class="string">'\''</span> +</span><br><span class="line">                <span class="string">", name='"</span> + name + <span class="string">'\''</span> +</span><br><span class="line">                <span class="string">", age='"</span> + age + <span class="string">'\''</span> +</span><br><span class="line">                <span class="string">", createTime="</span> + createTime +</span><br><span class="line">                <span class="string">", lastUpdateTime="</span> + lastUpdateTime +</span><br><span class="line">                <span class="string">'&#125;'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="1、SolrClient"><a href="#1、SolrClient" class="headerlink" title="1、SolrClient"></a>1、SolrClient</h3><p>说明：测试用到了两个库 ，分别是core和coername库 ，<br>创建来源：Solr 安装篇-创建core库：创建core库方式一(推荐)corename、创建core库方式二：cor 得到的。</p><p>SolrClientController.java</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.dist.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.alibaba.fastjson.JSON;</span><br><span class="line"><span class="keyword">import</span> com.alibaba.fastjson.serializer.SerializerFeature;</span><br><span class="line"><span class="keyword">import</span> com.dist.entity.UserEntity;</span><br><span class="line"><span class="keyword">import</span> com.dist.service.UserService;</span><br><span class="line"><span class="keyword">import</span> com.dist.util.PropertyNullToStrUtil;</span><br><span class="line"><span class="keyword">import</span> io.swagger.annotations.Api;</span><br><span class="line"><span class="keyword">import</span> io.swagger.annotations.ApiOperation;</span><br><span class="line"><span class="keyword">import</span> io.swagger.annotations.ApiParam;</span><br><span class="line"><span class="keyword">import</span> org.apache.solr.client.solrj.SolrClient;</span><br><span class="line"><span class="keyword">import</span> org.apache.solr.client.solrj.SolrQuery;</span><br><span class="line"><span class="keyword">import</span> org.apache.solr.client.solrj.SolrServerException;</span><br><span class="line"><span class="keyword">import</span> org.apache.solr.client.solrj.response.QueryResponse;</span><br><span class="line"><span class="keyword">import</span> org.apache.solr.client.solrj.response.UpdateResponse;</span><br><span class="line"><span class="keyword">import</span> org.apache.solr.common.SolrDocumentList;</span><br><span class="line"><span class="keyword">import</span> org.apache.solr.common.SolrInputDocument;</span><br><span class="line"><span class="keyword">import</span> org.json.JSONObject;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.scheduling.annotation.Scheduled;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMethod;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestParam;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RestController;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.text.SimpleDateFormat;</span><br><span class="line"><span class="keyword">import</span> java.time.LocalDateTime;</span><br><span class="line"><span class="keyword">import</span> java.time.format.DateTimeFormatter;</span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.Optional;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**全文检索 Solr SolrClient</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhengja@dist.com.cn</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@data</span> 2019/8/20 15:43</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="comment">//@EnableScheduling  //开启定时任务 ，项目启动自动执行定时任务</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(value = <span class="string">"rest/solr/v1"</span>)</span><br><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="meta">@Api</span>(tags = &#123;<span class="string">"SolrClientController"</span>&#125;,description = <span class="string">"Solr 全文检索 SolrClient"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SolrClientController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//方式一</span></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> SolrClient client;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//数据库获取数据接口-对应实体类:UserEntity</span></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> UserService userService;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"core库-添加文档内容-方式一"</span>,notes = <span class="string">"向文档中添加域 ，必须有id域 ，域的名称必须在scheme.xml中定义"</span>,httpMethod = <span class="string">"GET"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"core/insert"</span>,method = RequestMethod.GET)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">validator</span><span class="params">(@ApiParam(value = <span class="string">"name"</span>)</span> @RequestParam String name,</span></span><br><span class="line"><span class="function">                            @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"age"</span>)</span> @RequestParam String age,</span></span><br><span class="line"><span class="function">                            @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"默 core 库"</span> ,defaultValue = <span class="string">"core"</span>)</span> @RequestParam String collection)</span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            String idStr = String.valueOf(System.currentTimeMillis());</span><br><span class="line"></span><br><span class="line">            SolrInputDocument document = <span class="keyword">new</span> SolrInputDocument();</span><br><span class="line">            document.setField(<span class="string">"id"</span>, idStr);</span><br><span class="line">            document.setField(<span class="string">"name"</span>, name);</span><br><span class="line">            document.setField(<span class="string">"age"</span>,age);</span><br><span class="line"></span><br><span class="line">            <span class="comment">//1.可以用addBean 添加对象写入索引库-(推荐使用)-不存在null指针异常</span></span><br><span class="line">            <span class="comment">/*UserEntityDto userEntityDto = new UserEntityDto();</span></span><br><span class="line"><span class="comment">            userEntityDto.setId(10L);</span></span><br><span class="line"><span class="comment">            userEntityDto.setName("2"+name);</span></span><br><span class="line"><span class="comment">            userEntityDto.setCreateTime(new Date());</span></span><br><span class="line"><span class="comment">            client.addBean(collection,userEntityDto);*/</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">//2.也可以把文档对象写入索引库</span></span><br><span class="line">            client.add(collection,document);<span class="comment">//如果配置文件中没有指定core ，这个方法的第一个参数就需要指定core名称,比如client.add("core", doc);</span></span><br><span class="line">            client.commit(collection);<span class="comment">//如果配置文件中没有指定core ，这个方法的第一个参数就需要指定core名称client.commit("core");</span></span><br><span class="line">            <span class="keyword">return</span> idStr;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"error"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**怎么保证是修改还是新增呢？</span></span><br><span class="line"><span class="comment">     * 这里主要是根据id来判断 ，这个id类似db中的唯一主键 ，当我们没有指定id时 ，会随机生成一个id</span></span><br><span class="line"><span class="comment">     *如果存在相同的id ，则修改文档；如果不存在 ，则新增文档</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"core库-更新文档-方式一"</span>,notes = <span class="string">"更新文档内容 ，跟添加的区别是：id不能变 ，其他的可以变"</span>,httpMethod = <span class="string">"GET"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"core/updateDocument"</span>,method = RequestMethod.GET)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">updateDocument</span><span class="params">(@ApiParam(value = <span class="string">"idStr"</span>)</span> @RequestParam String idStr,</span></span><br><span class="line"><span class="function">                               @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"name"</span>)</span> @RequestParam String name,</span></span><br><span class="line"><span class="function">                               @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"age"</span>)</span> @RequestParam String age,</span></span><br><span class="line"><span class="function">                                 @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"sex2"</span>)</span> @RequestParam String sex2,</span></span><br><span class="line"><span class="function">                                 @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"默 core 库"</span> ,defaultValue = <span class="string">"core"</span>)</span> @RequestParam String collection) <span class="keyword">throws</span> Exception</span>&#123;</span><br><span class="line">        <span class="comment">// 创建一个文档对象, 向文档中添加域 ，必须有id域 ，域的名称必须在scheme.xml中定义</span></span><br><span class="line">        SolrInputDocument document = <span class="keyword">new</span> SolrInputDocument();</span><br><span class="line">        document.setField(<span class="string">"id"</span>, idStr);</span><br><span class="line">        document.setField(<span class="string">"name"</span>, name);</span><br><span class="line">        document.setField(<span class="string">"age"</span>,age);</span><br><span class="line">        document.setField(<span class="string">"sex2"</span>,sex2);</span><br><span class="line">        <span class="comment">// 把文档对象写入索引库</span></span><br><span class="line">        client.add(collection,document);</span><br><span class="line">        <span class="comment">// 提交</span></span><br><span class="line">        client.commit(collection);</span><br><span class="line">        <span class="keyword">return</span> document;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"查询文档内容-方式一"</span>,notes = <span class="string">"复杂查询"</span>,httpMethod = <span class="string">"GET"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"queryDocument"</span>,method = RequestMethod.GET)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">queryDocument</span><span class="params">(@ApiParam(value = <span class="string">"条件"</span>,defaultValue = <span class="string">"*:*"</span>)</span> @RequestParam String condition,</span></span><br><span class="line"><span class="function">                              @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"core/默 corename 库"</span>,defaultValue = <span class="string">"corename"</span>)</span> @RequestParam String collection,</span></span><br><span class="line"><span class="function">                              @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"分页起始 默 0"</span>,defaultValue = <span class="string">"0"</span>)</span> @RequestParam Integer pageStart,</span></span><br><span class="line"><span class="function">                              @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"分页结束 默 10"</span>,defaultValue = <span class="string">"10"</span>)</span> @RequestParam Integer pageEnd) <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="comment">// 创建一个查询条件</span></span><br><span class="line">        SolrQuery solrQuery = <span class="keyword">new</span> SolrQuery();</span><br><span class="line">        <span class="comment">// 设置查询条件</span></span><br><span class="line">        solrQuery.setQuery(condition);</span><br><span class="line">        <span class="comment">// 设置分页</span></span><br><span class="line">        solrQuery.setStart(pageStart);</span><br><span class="line">        solrQuery.setRows(pageEnd);</span><br><span class="line">        <span class="comment">//排序</span></span><br><span class="line">        solrQuery.setSort(<span class="string">"id"</span>,SolrQuery.ORDER.asc);</span><br><span class="line"></span><br><span class="line">        <span class="comment">/*// df 代表默认的查询字段</span></span><br><span class="line"><span class="comment">        solrQuery.set("name", "关键字");</span></span><br><span class="line"><span class="comment">        //   指的是你查询完毕之后要返回的字段</span></span><br><span class="line"><span class="comment">        solrQuery.set("name", "id,name");</span></span><br><span class="line"><span class="comment">        //高亮</span></span><br><span class="line"><span class="comment">        //打开开关</span></span><br><span class="line"><span class="comment">        solrQuery.setHighlight(false);</span></span><br><span class="line"><span class="comment">        solrQuery.addHighlightField("name"); // 高亮字段</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        //设置前缀</span></span><br><span class="line"><span class="comment">        solrQuery.setHighlightSimplePre("&lt;font color=\"red\"&gt;");</span></span><br><span class="line"><span class="comment">        //设置后缀</span></span><br><span class="line"><span class="comment">        solrQuery.setHighlightSimplePost("&lt;/font&gt;");*/</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 执行查询</span></span><br><span class="line">        QueryResponse query = client.query(collection,solrQuery);</span><br><span class="line">        <span class="comment">// 取查询结果</span></span><br><span class="line">        SolrDocumentList solrDocumentList = query.getResults();</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">"总记录数："</span> + solrDocumentList.getNumFound());</span><br><span class="line">        client.commit(collection);</span><br><span class="line">        <span class="keyword">return</span> solrDocumentList;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"根据id删除文档--方式一"</span>,notes = <span class="string">"根据id删除单个文档"</span>,httpMethod = <span class="string">"DELETE"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"deteleDocument"</span>,method = RequestMethod.DELETE)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">deteleDocument</span><span class="params">(@ApiParam(value = <span class="string">"core/默 corename 库"</span> ,defaultValue = <span class="string">"corename"</span>)</span> @RequestParam String collection,</span></span><br><span class="line"><span class="function">                               @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"idStr"</span>)</span> @RequestParam String idStr) <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 根据条件删除</span></span><br><span class="line">        <span class="comment">// httpSolrServer.deleteByQuery("");</span></span><br><span class="line">        <span class="comment">// 根据id删除</span></span><br><span class="line">        UpdateResponse response = client.deleteById(collection, idStr);</span><br><span class="line">        <span class="comment">// 提交</span></span><br><span class="line">        client.commit(collection);</span><br><span class="line">        <span class="keyword">return</span> response;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"根据条件删除文档-方式一"</span>,notes = <span class="string">"默认删除所有文档"</span>,httpMethod = <span class="string">"DELETE"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"deteleAllDocument"</span>,method = RequestMethod.DELETE)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">deteleAllDocument</span><span class="params">(@ApiParam(value = <span class="string">"core/默 corename 库"</span> ,defaultValue = <span class="string">"corename"</span>)</span> @RequestParam String collection,</span></span><br><span class="line"><span class="function">                                    @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"条件"</span>,defaultValue = <span class="string">"*:*"</span>)</span> @RequestParam String condition) <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 根据条件删除</span></span><br><span class="line">        <span class="comment">// httpSolrServer.deleteByQuery("");</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 删除所有文档</span></span><br><span class="line">        UpdateResponse response = client.deleteByQuery(collection,condition);</span><br><span class="line">        <span class="comment">// 提交</span></span><br><span class="line">        client.commit(collection);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> <span class="string">"删除所有文档-成功！"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"corename库-添加文档内容-方式一"</span>,notes = <span class="string">"向文档中添加域 ，必须有id域 ，域的名称必须在scheme.xml中定义"</span>,httpMethod = <span class="string">"GET"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"corename/insert"</span>,method = RequestMethod.GET)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">validator2</span><span class="params">(@ApiParam(value = <span class="string">"数据库数据id"</span>)</span> @RequestParam Long id,</span></span><br><span class="line"><span class="function">                             @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"默 corename 库"</span> ,defaultValue = <span class="string">"corename"</span>)</span> @RequestParam String collection) <span class="keyword">throws</span> IOException, SolrServerException </span>&#123;</span><br><span class="line">        Optional&lt;UserEntity&gt; userEntity = <span class="keyword">this</span>.userService.getUserById(id);</span><br><span class="line">        UserEntity user = userEntity.get();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//方式一(推荐使用)：把数据对象写入索引库  -不存在null指针异常</span></span><br><span class="line">        <span class="comment">//client.addBean(collection,user);</span></span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">"user "</span>+user);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//方式二：把文档对象写入索引库</span></span><br><span class="line">        SolrInputDocument document = <span class="keyword">new</span> SolrInputDocument();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//原始添加方式：这种方式也可以 ，不过类属性字段 ，不能有null值存在 ，如 age=null ，出错：null指针异常</span></span><br><span class="line">        <span class="comment">/*document.addField("user_id",user.getId().toString());</span></span><br><span class="line"><span class="comment">        document.addField("user_age",user.getAge().toString());</span></span><br><span class="line"><span class="comment">        document.addField("user_lastUpdateTime",new SimpleDateFormat("yyyy-MM-dd HH:MM:SS").format(new Date()));</span></span><br><span class="line"><span class="comment">        document.addField("user_name",user.getName().toString());*/</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//解决属性值 null 指针异常</span></span><br><span class="line">        <span class="comment">//把 类 中所有属性为 null 的转为 ""</span></span><br><span class="line">        String str = JSON.toJSONString(user,SerializerFeature.WriteMapNullValue);</span><br><span class="line">        <span class="comment">//json对象转string</span></span><br><span class="line">        JSONObject object = <span class="keyword">new</span> JSONObject(str);</span><br><span class="line">        JSONObject jsonObject = PropertyNullToStrUtil.filterNull(object);</span><br><span class="line">        System.out.println(<span class="string">"jsonObject"</span>+jsonObject);</span><br><span class="line"></span><br><span class="line">        document.addField(<span class="string">"id"</span>,jsonObject.get(<span class="string">"id"</span>));</span><br><span class="line">        document.addField(<span class="string">"user_age"</span>,jsonObject.get(<span class="string">"age"</span>));</span><br><span class="line">        document.addField(<span class="string">"user_lastUpdateTime"</span>,<span class="keyword">new</span> SimpleDateFormat(<span class="string">"yyyy-MM-dd HH:MM:SS"</span>).format(<span class="keyword">new</span> Date()));</span><br><span class="line">        document.addField(<span class="string">"user_name"</span>,jsonObject.get(<span class="string">"name"</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">//方式二：把文档对象写入索引库</span></span><br><span class="line">        client.add(collection,document);<span class="comment">//如果配置文件中没有指定core ，这个方法的第一个参数就需要指定core名称,比如client.add("corename", doc);</span></span><br><span class="line">        client.commit(collection);<span class="comment">//如果配置文件中没有指定core ，这个方法的第一个参数就需要指定core名称client.commit("core");</span></span><br><span class="line">        <span class="keyword">return</span> document;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"corename库-更新文档内容-方式一"</span>,notes = <span class="string">"更新文档内容 ，跟添加的区别是：id不能变 ，其他的可以变"</span>,httpMethod = <span class="string">"GET"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"corename/updateDocument"</span>,method = RequestMethod.GET)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">updateDocument2</span><span class="params">(@ApiParam(value = <span class="string">"数据库数据id"</span>)</span> @RequestParam Long id,</span></span><br><span class="line"><span class="function">                                  @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"Solr数据库id"</span>)</span> @RequestParam String solrid,</span></span><br><span class="line"><span class="function">                                  @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"默 corename 库"</span> ,defaultValue = <span class="string">"corename"</span>)</span> @RequestParam String collection) <span class="keyword">throws</span> Exception</span>&#123;</span><br><span class="line">        Optional&lt;UserEntity&gt; userEntity = <span class="keyword">this</span>.userService.getUserById(id);</span><br><span class="line">        UserEntity user = userEntity.get();</span><br><span class="line">        <span class="comment">// 创建一个文档对象, 向文档中添加域 ，必须有id域 ，域的名称必须在scheme.xml中定义</span></span><br><span class="line">        SolrInputDocument document = <span class="keyword">new</span> SolrInputDocument();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//把 类 中所有属性为 null 的转为 ""</span></span><br><span class="line">        String str = JSON.toJSONString(user,SerializerFeature.WriteMapNullValue);</span><br><span class="line">        <span class="comment">//json对象转string</span></span><br><span class="line">        JSONObject object = <span class="keyword">new</span> JSONObject(str);</span><br><span class="line">        JSONObject jsonObject = PropertyNullToStrUtil.filterNull(object);</span><br><span class="line">        <span class="comment">//System.out.println("jsonObject"+jsonObject);</span></span><br><span class="line">        document.addField(<span class="string">"id"</span>,jsonObject.get(<span class="string">"id"</span>));</span><br><span class="line">        document.addField(<span class="string">"user_age"</span>,jsonObject.get(<span class="string">"age"</span>));</span><br><span class="line">        document.addField(<span class="string">"user_lastUpdateTime"</span>,jsonObject.get(<span class="string">"lastUpdateTime"</span>));</span><br><span class="line">        document.addField(<span class="string">"user_name"</span>,jsonObject.get(<span class="string">"name"</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 把文档对象写入索引库</span></span><br><span class="line">        client.add(collection,document);</span><br><span class="line">        <span class="comment">// 提交</span></span><br><span class="line">        client.commit(collection);</span><br><span class="line">        <span class="keyword">return</span> document;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"corename库-将数据库的数据导入solr索引库-方式一"</span>,notes = <span class="string">"将数据库的数据导入solr索引库"</span>,httpMethod = <span class="string">"GET"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"dataToSolr"</span>,method = RequestMethod.GET)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">dataToSolr</span><span class="params">(@ApiParam(value = <span class="string">"默 corename 库"</span> ,defaultValue = <span class="string">"corename"</span>)</span> @RequestParam String collection) <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="comment">//先去数据库查数据</span></span><br><span class="line">        List&lt;UserEntity&gt; userList = <span class="keyword">this</span>.userService.getUserList();</span><br><span class="line">        <span class="comment">//循环遍历查询</span></span><br><span class="line">        <span class="keyword">for</span> (UserEntity user : userList)&#123;</span><br><span class="line"></span><br><span class="line">            SolrInputDocument document = <span class="keyword">new</span> SolrInputDocument();</span><br><span class="line">            <span class="comment">//创建文档对象</span></span><br><span class="line">            <span class="comment">//添加域</span></span><br><span class="line">            <span class="comment">//System.out.println("user= "+user);</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">//把 类 中所有属性为 null 的转为 ""</span></span><br><span class="line">            String str = JSON.toJSONString(user,SerializerFeature.WriteMapNullValue);</span><br><span class="line">            <span class="comment">//json对象转string</span></span><br><span class="line">            JSONObject object = <span class="keyword">new</span> JSONObject(str);</span><br><span class="line">            JSONObject jsonObject = PropertyNullToStrUtil.filterNull(object);</span><br><span class="line">            <span class="comment">//System.out.println("jsonObject"+jsonObject);</span></span><br><span class="line">            document.addField(<span class="string">"id"</span>,jsonObject.get(<span class="string">"id"</span>));</span><br><span class="line">            document.addField(<span class="string">"user_age"</span>,jsonObject.get(<span class="string">"age"</span>));</span><br><span class="line">            document.addField(<span class="string">"user_lastUpdateTime"</span>,jsonObject.get(<span class="string">"lastUpdateTime"</span>));</span><br><span class="line">            document.addField(<span class="string">"user_name"</span>,jsonObject.get(<span class="string">"name"</span>));</span><br><span class="line">            <span class="comment">//写入</span></span><br><span class="line">            System.out.println(<span class="string">"document="</span>+document);</span><br><span class="line">            client.add(collection,document);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//提交</span></span><br><span class="line">        client.commit(collection);</span><br><span class="line">        System.out.println(<span class="string">"成功保存数据到corename库："</span>+userList);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**需要在类上 加开启定时任务注解  <span class="doctag">@EnableScheduling</span></span></span><br><span class="line"><span class="comment">     * 定时器  - 全量更新 -成功</span></span><br><span class="line"><span class="comment">     * cron代表的是时间  如下代表（每隔2分钟执行一次）</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Scheduled</span>(cron = <span class="string">"0 */2 * * * ?"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">timer</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="comment">//获取当前时间</span></span><br><span class="line">        LocalDateTime localDateTime = LocalDateTime.now();</span><br><span class="line">        <span class="comment">//输出当前时间</span></span><br><span class="line">        System.out.println(<span class="string">"当前时间为:"</span> +</span><br><span class="line">                localDateTime.format(DateTimeFormatter.ofPattern(<span class="string">"yyyy-MM-dd HH:mm:ss"</span>)));</span><br><span class="line">        System.out.println(<span class="string">"============测试一============"</span>);</span><br><span class="line">        <span class="comment">//调用删除索引的方法</span></span><br><span class="line">        deteleAllDocument(<span class="string">"corename"</span>,<span class="string">"*:*"</span>);</span><br><span class="line">        Thread.sleep(<span class="number">5000</span>);</span><br><span class="line">        <span class="comment">//调用数据新增到索引库的方法</span></span><br><span class="line">        dataToSolr(<span class="string">"corename"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="2-SolrTemplate"><a href="#2-SolrTemplate" class="headerlink" title="2.SolrTemplate"></a>2.SolrTemplate</h3><p>说明：测试用到了两个库 ，分别是core和coername库 ，<br>创建来源：Solr 安装篇-创建core库：创建core库方式一(推荐)corename、创建core库方式二：cor 得到的。</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.dist.config;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.apache.solr.client.solrj.SolrClient;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Bean;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.solr.core.SolrTemplate;</span><br><span class="line"></span><br><span class="line"><span class="comment">/** solr 全文检索自动配置 ==自动装配</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhengja@dist.com.cn</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@data</span> 2019/8/20 15:36</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SearchAutoConfig</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="meta">@ConditionalOnMissingBean</span>(SolrTemplate<span class="class">.<span class="keyword">class</span>)</span></span><br><span class="line"><span class="class">    <span class="title">public</span> <span class="title">SolrTemplate</span> <span class="title">solrTemplate</span>(<span class="title">SolrClient</span> <span class="title">solrClient</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> SolrTemplate(solrClient);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>SolrTemplateController.java</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.dist.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.dist.entity.UserEntity;</span><br><span class="line"><span class="keyword">import</span> com.dist.entity.UserEntityDto;</span><br><span class="line"><span class="keyword">import</span> com.dist.service.UserService;</span><br><span class="line"><span class="keyword">import</span> io.swagger.annotations.Api;</span><br><span class="line"><span class="keyword">import</span> io.swagger.annotations.ApiOperation;</span><br><span class="line"><span class="keyword">import</span> io.swagger.annotations.ApiParam;</span><br><span class="line"><span class="keyword">import</span> org.apache.solr.client.solrj.response.UpdateResponse;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.solr.core.SolrTemplate;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.solr.core.query.Query;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.solr.core.query.SimpleQuery;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.solr.core.query.result.ScoredPage;</span><br><span class="line"><span class="keyword">import</span> org.springframework.scheduling.annotation.Scheduled;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.*;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.time.LocalDateTime;</span><br><span class="line"><span class="keyword">import</span> java.time.format.DateTimeFormatter;</span><br><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.Optional;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**全文检索 Solr</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhengja@dist.com.cn</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@data</span> 2019/8/20 15:43</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="comment">//@EnableScheduling  //开启定时任务 ，项目启动自动执行定时任务</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(value = <span class="string">"rest/solr/v2"</span>)</span><br><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="meta">@Api</span>(tags = &#123;<span class="string">"SolrTemplateController"</span>&#125;,description = <span class="string">"Solr 全文检索 SolrTemplate"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SolrTemplateController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//方式二：自动装配</span></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> SolrTemplate solrTemplate;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//数据库获取数据接口-对应实体类:UserEntity</span></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> UserService userService;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"core库-添加文档内容-方式二"</span>,notes = <span class="string">"向文档中添加域 ，必须有id域 ，域的名称必须在scheme.xml中定义"</span>,httpMethod = <span class="string">"POST"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"v2/core/insert"</span>,method = RequestMethod.POST)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">validator2</span><span class="params">(@ApiParam(value = <span class="string">"具体参考：Model"</span>)</span> @RequestBody UserEntityDto userEntityDto,</span></span><br><span class="line"><span class="function">                             @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"默 core 库"</span> ,defaultValue = <span class="string">"core"</span>)</span> @RequestParam String collection)</span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            UpdateResponse response = <span class="keyword">this</span>.solrTemplate.saveBean(collection, userEntityDto);</span><br><span class="line">            solrTemplate.commit(collection);</span><br><span class="line">            <span class="keyword">return</span> response;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"error"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"core库-更新文档-方式二"</span>,notes = <span class="string">"更新文档内容 ，跟添加的区别是：id不能变 ，其他的可以变"</span>,httpMethod = <span class="string">"PUT"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"v2/core/updateDocument"</span>,method = RequestMethod.PUT)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">updateDocument2</span><span class="params">(@ApiParam(value = <span class="string">"具体参考：Model"</span>)</span> @RequestBody UserEntityDto userEntityDto,</span></span><br><span class="line"><span class="function">                                  @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"默 core 库"</span> ,defaultValue = <span class="string">"core"</span>)</span> @RequestParam String collection) <span class="keyword">throws</span> Exception</span>&#123;</span><br><span class="line">        UpdateResponse updateResponse = <span class="keyword">this</span>.solrTemplate.saveBean(collection, userEntityDto);</span><br><span class="line">        <span class="keyword">this</span>.solrTemplate.commit(collection);</span><br><span class="line">        <span class="keyword">return</span> updateResponse;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"core库-批量更新文档-方式二"</span>,notes = <span class="string">"批量更新文档"</span>,httpMethod = <span class="string">"GET"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"v3/core/updateDocument"</span>,method = RequestMethod.GET)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">updateDocument3</span><span class="params">(@ApiParam(value = <span class="string">"默 core 库"</span> ,defaultValue = <span class="string">"core"</span>)</span> @RequestParam String collection)</span>&#123;</span><br><span class="line"></span><br><span class="line">        UserEntityDto document = <span class="keyword">new</span> UserEntityDto();</span><br><span class="line">        document.setId(<span class="number">1L</span>);</span><br><span class="line">        document.setAge(<span class="string">"20"</span>);</span><br><span class="line">        document.setGuid(<span class="string">"123"</span>);</span><br><span class="line">        <span class="comment">//少name字段</span></span><br><span class="line"></span><br><span class="line">        UserEntityDto document2 = <span class="keyword">new</span> UserEntityDto();</span><br><span class="line">        document2.setId(<span class="number">2L</span>);</span><br><span class="line">        document2.setAge(<span class="string">"21"</span>);</span><br><span class="line">        document2.setName(<span class="string">"List2"</span>);</span><br><span class="line">        document2.setGuid(<span class="string">"1234"</span>);  <span class="comment">//多guid字段</span></span><br><span class="line"></span><br><span class="line">        UserEntityDto document3 = <span class="keyword">new</span> UserEntityDto();</span><br><span class="line">        document3.setId(<span class="number">3L</span>);</span><br><span class="line">        document3.setAge(<span class="string">"22"</span>);</span><br><span class="line">        document3.setName(<span class="string">"List3"</span>);</span><br><span class="line">        <span class="comment">//正常字段</span></span><br><span class="line"></span><br><span class="line">        UpdateResponse response = solrTemplate.saveBeans(collection, Arrays.asList(document, document2,document3));</span><br><span class="line">        solrTemplate.commit(collection);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> response;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"查询文档内容-方式二"</span>,notes = <span class="string">"复杂(高级)查询"</span>,httpMethod = <span class="string">"GET"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"v2/queryDocument"</span>,method = RequestMethod.GET)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">queryDocument2</span><span class="params">(@ApiParam(value = <span class="string">"条件"</span>,defaultValue = <span class="string">"*:*"</span>)</span> @RequestParam String condition,</span></span><br><span class="line"><span class="function">                                 @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"corename/默 core 库"</span>,defaultValue = <span class="string">"core"</span>)</span> @RequestParam String collection,</span></span><br><span class="line"><span class="function">                                 @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"分页起始 默 0"</span>,defaultValue = <span class="string">"0"</span>)</span> @RequestParam Long pageStart,</span></span><br><span class="line"><span class="function">                                 @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"分页结束 默 10"</span>,defaultValue = <span class="string">"10"</span>)</span> @RequestParam Integer pageEnd) <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line"></span><br><span class="line">        Query query=<span class="keyword">new</span> SimpleQuery(condition);</span><br><span class="line">        query.setOffset(pageStart);  <span class="comment">//开始索引（默认0）start:(page-1)*rows</span></span><br><span class="line">        query.setRows(pageEnd);      <span class="comment">//每页记录数(默认10)//rows:rows</span></span><br><span class="line">        <span class="keyword">if</span> (collection.equals(<span class="string">"core"</span>))&#123;</span><br><span class="line">            ScoredPage&lt;UserEntityDto&gt; userEntityDtos = <span class="keyword">this</span>.solrTemplate.queryForPage(collection, query, UserEntityDto<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line">            <span class="keyword">this</span>.solrTemplate.commit(collection);</span><br><span class="line">            <span class="keyword">return</span> userEntityDtos.iterator();</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            ScoredPage&lt;UserEntity&gt; userEntities = <span class="keyword">this</span>.solrTemplate.queryForPage(collection, query, UserEntity<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line">            <span class="keyword">this</span>.solrTemplate.commit(collection);</span><br><span class="line">            <span class="keyword">return</span> userEntities.iterator();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"根据id查询文档内容-方式二"</span>,notes = <span class="string">"根据id查询文档内容"</span>,httpMethod = <span class="string">"GET"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"v3/queryDocument"</span>,method = RequestMethod.GET)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">queryDocument3</span><span class="params">(@ApiParam(value = <span class="string">"条件id"</span>)</span> @RequestParam String id,</span></span><br><span class="line"><span class="function">                                 @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"corename/默 core 库"</span>,defaultValue = <span class="string">"core"</span>)</span> @RequestParam String collection)</span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (collection.equals(<span class="string">"core"</span>))&#123;</span><br><span class="line">            Optional&lt;UserEntityDto&gt; userEntityDto = <span class="keyword">this</span>.solrTemplate.getById(collection, id, UserEntityDto<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line">            System.out.println(<span class="string">"userEntityDto "</span>+userEntityDto);</span><br><span class="line">            <span class="keyword">this</span>.solrTemplate.commit(collection);</span><br><span class="line">            <span class="keyword">return</span> userEntityDto.get();</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            Optional&lt;UserEntity&gt; userEntity = <span class="keyword">this</span>.solrTemplate.getById(collection, id, UserEntity<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line">            System.out.println(<span class="string">"userEntity "</span>+userEntity);</span><br><span class="line">            <span class="keyword">this</span>.solrTemplate.commit(collection);</span><br><span class="line">            <span class="keyword">return</span> userEntity.get();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"corename库-将数据库的数据导入solr索引库-方式二"</span>,notes = <span class="string">"将数据库的数据导入solr索引库"</span>,httpMethod = <span class="string">"GET"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"v2/dataToSolr"</span>,method = RequestMethod.GET)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">dataToSolr2</span><span class="params">(@ApiParam(value = <span class="string">"默 corename 库"</span> ,defaultValue = <span class="string">"corename"</span>)</span> @RequestParam String collection) <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="comment">//先去数据库查数据</span></span><br><span class="line">        List&lt;UserEntity&gt; userList = <span class="keyword">this</span>.userService.getUserList();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//同时 ，解决了属性值 null 报错的问题</span></span><br><span class="line">        UpdateResponse updateResponse = <span class="keyword">this</span>.solrTemplate.saveBeans(collection, userList);</span><br><span class="line">        System.out.println(<span class="string">"成功保存数据到corename库："</span>+userList);</span><br><span class="line">        <span class="keyword">this</span>.solrTemplate.commit(collection);</span><br><span class="line">        <span class="keyword">return</span> updateResponse;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"corename库-增量/默认全量更新-数据库-方式二"</span>,notes = <span class="string">"java操作Solr的全量或增量更新 ，可以结合定时任务做定时全量或增量更新"</span>,httpMethod = <span class="string">"GET"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"updateSolrData"</span>,method = RequestMethod.GET)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">updateSolrData</span><span class="params">(@ApiParam(value = <span class="string">"默 corename 库"</span>,defaultValue = <span class="string">"corename"</span>)</span> @RequestParam String collection,</span></span><br><span class="line"><span class="function">                                 @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"默 全量更新"</span>,defaultValue = <span class="string">"true"</span>)</span> @RequestParam String is) <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="comment">//获取当前时间</span></span><br><span class="line">        LocalDateTime localDateTime = LocalDateTime.now();</span><br><span class="line">        <span class="comment">//输出当前时间</span></span><br><span class="line">        System.out.println(<span class="string">"当前时间为:"</span> + localDateTime.format(DateTimeFormatter.ofPattern(<span class="string">"yyyy-MM-dd HH:mm:ss"</span>)));</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (is.equals(<span class="string">"true"</span>))&#123;</span><br><span class="line">            <span class="comment">//调用删除索引的方法 ，实现全量更新-不加的 ，多次调用就是增量更新</span></span><br><span class="line">            deteleAllDocument2(collection,<span class="string">"*:*"</span>);</span><br><span class="line">            System.out.println(<span class="string">"============测试全量更新============"</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">"============测试增量更新============"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//调用数据新增到索引库的方法二</span></span><br><span class="line">        Object toSolr2 = dataToSolr2(collection);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> toSolr2;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"根据条件删除文档"</span>,notes = <span class="string">"默认删除所有文档"</span>,httpMethod = <span class="string">"DELETE"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"v2/deteleAllDocument"</span>,method = RequestMethod.DELETE)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">deteleAllDocument2</span><span class="params">(@ApiParam(value = <span class="string">"core/默 corename 库"</span> ,defaultValue = <span class="string">"corename"</span>)</span> @RequestParam String collection,</span></span><br><span class="line"><span class="function">                                    @<span class="title">ApiParam</span><span class="params">(value = <span class="string">"条件"</span>,defaultValue = <span class="string">"*:*"</span>)</span> @RequestParam String condition) <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 根据条件删除</span></span><br><span class="line">        <span class="comment">// httpSolrServer.deleteByQuery("");</span></span><br><span class="line">        Query query=<span class="keyword">new</span> SimpleQuery(condition);</span><br><span class="line">        <span class="comment">// 删除所有文档</span></span><br><span class="line">        UpdateResponse response = <span class="keyword">this</span>.solrTemplate.delete(collection,query);</span><br><span class="line">        <span class="comment">// 提交</span></span><br><span class="line">        <span class="keyword">this</span>.solrTemplate.commit(collection);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> <span class="string">"删除文档-成功！"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 需要在类上 加开启定时任务注解  <span class="doctag">@EnableScheduling</span></span></span><br><span class="line"><span class="comment">     * 全量更新 -成功</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Scheduled</span>(cron = <span class="string">"0 */1 * * * ?"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">timer2</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="comment">//获取当前时间</span></span><br><span class="line">        LocalDateTime localDateTime = LocalDateTime.now();</span><br><span class="line">        <span class="comment">//输出当前时间</span></span><br><span class="line">        System.out.println(<span class="string">"当前时间为:"</span> +</span><br><span class="line">                localDateTime.format(DateTimeFormatter.ofPattern(<span class="string">"yyyy-MM-dd HH:mm:ss"</span>)));</span><br><span class="line">        System.out.println(<span class="string">"============测试二============"</span>);</span><br><span class="line">        <span class="comment">//调用删除索引的方法</span></span><br><span class="line">        deteleAllDocument2(<span class="string">"corename"</span>,<span class="string">"*:*"</span>);</span><br><span class="line">        Thread.sleep(<span class="number">5000</span>);</span><br><span class="line">        <span class="comment">//调用数据新增到索引库的方法二</span></span><br><span class="line">        dataToSolr2(<span class="string">"corename"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>3、Solr加用户后密码的配置</p><p>具体配置用户验证参考：[Solr 配置用户登录验证.md](Solr 配置用户登录验证.md)<br>springboot通过solr地址中加入用户名密码的方式连接 ，地址如下：</p><p>*.yml文件</p><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring:</span></span><br><span class="line">  <span class="attr">data:</span></span><br><span class="line">      <span class="attr">solr:</span>  <span class="comment"># 全文检索</span></span><br><span class="line">        <span class="comment"># solr加上了用户名密码访问条件 ，参数中并没有地方设置username和password ，那应该怎么办?</span></span><br><span class="line">        <span class="attr">host:</span>  <span class="string">http://user:pass@127.0.0.1:8983/solr</span></span><br></pre></td></tr></table></figure></div><div class="popular-posts-header"><p style="font-size:18px;color:red;margin:0 0 5px 0">相关文章推荐：</p></div><ul class="popular-posts"><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\1601327121.html" rel="bookmark">Solr-界面功能</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\1104927974.html" rel="bookmark">Solr-安装和使用目录篇</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\2166623843.html" rel="bookmark">Solr-配置用户登录验证</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\2717257258.html" rel="bookmark">Solr-安装篇</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\379120075.html" rel="bookmark">Solr全量更新-增量更新(要配置数据库)</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\3847429115.html" rel="bookmark">solr-安装中文分词器安装和配置</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\1689906707.html" rel="bookmark">Solr-连接数据库</a></div></li></ul><div><div style="text-align:center;color:#ccc;font-size:14px">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div></div><div class="reward-container"><div><p style="font-size:14px;color:#34495e;margin:0 0 5px 0">赞赏一下吧～ 还可以关注公众号订阅最新内容</p></div><button disable="enable" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">打赏</button><div id="qr" style="display:none"><div style="display:inline-block"><img src="/my-images/WeChat.png" alt="宇宙小神特别萌 微信支付"><p>微信支付</p></div><div style="display:inline-block"><img src="/my-images/Alipay.png" alt="宇宙小神特别萌 支付宝"><p>支付宝</p></div></div></div><footer class="post-footer"><div class="post-tags"><a href="/tags/Solr/" rel="tag"><i class="fa fa-tag"></i> Solr</a></div><div class="post-nav"><div class="post-nav-item"><a href="/posts/1689906707.html" rel="prev" title="Solr-连接数据库"><i class="fa fa-chevron-left"></i> Solr-连接数据库</a></div><div class="post-nav-item"><a href="/posts/3847429115.html" rel="next" title="solr-安装中文分词器安装和配置">solr-安装中文分词器安装和配置 <i class="fa fa-chevron-right"></i></a></div></div></footer></article></div></div><div class="comments" id="valine-comments"></div><script>window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }</script></div><div class="toggle sidebar-toggle"><span class="toggle-line toggle-line-first"></span> <span class="toggle-line toggle-line-middle"></span> <span class="toggle-line toggle-line-last"></span></div><aside class="sidebar"><div class="sidebar-inner"><ul class="sidebar-nav motion-element"><li class="sidebar-nav-toc">文章目录</li><li class="sidebar-nav-overview">站点概览</li></ul><div class="post-toc-wrap sidebar-panel"><div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#一、springboot-solr简单测试"><span class="nav-text">一、springboot+solr简单测试</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1、创建springboot项目和测试代码"><span class="nav-text">1、创建springboot项目和测试代码</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#其他问题："><span class="nav-text">其他问题：</span></a></li></ol></li></ol></li></ol><li class="nav-item nav-level-2"><a class="nav-link" href="#二、升级版测试"><span class="nav-text">二、升级版测试</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1、SolrClient"><span class="nav-text">1、SolrClient</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-SolrTemplate"><span class="nav-text">2.SolrTemplate</span></a></li></ol></li></div></div><div class="site-overview-wrap sidebar-panel"><div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person"><img class="site-author-image" itemprop="image" alt="宇宙小神特别萌" src="/my-images/avatar.gif"><p class="site-author-name" itemprop="name">宇宙小神特别萌</p><div class="site-description" itemprop="description">人生就是一次幸运的旅行！</div></div><div class="site-state-wrap motion-element"><nav class="site-state"><div class="site-state-item site-state-posts"><a href="/archives/"><span class="site-state-item-count">116</span> <span class="site-state-item-name">文章</span></a></div><div class="site-state-item site-state-tags"><a href="/tags/"><span class="site-state-item-count">27</span> <span class="site-state-item-name">标签</span></a></div></nav></div><div class="links-of-author motion-element"><span class="links-of-author-item"><a href="https://github.com/zhengjiaao" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;zhengjiaao" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a> </span><span class="links-of-author-item"><a href="https://www.jianshu.com/u/70d69269bd09" title="简书 → https:&#x2F;&#x2F;www.jianshu.com&#x2F;u&#x2F;70d69269bd09" rel="noopener" target="_blank"><i class="fa fa-fw fa-book"></i>简书</a> </span><span class="links-of-author-item"><a href="/atom.xml" title="RSS → &#x2F;atom.xml"><i class="fa fa-fw fa-rss"></i>RSS</a></span></div><div class="links-of-blogroll motion-element"><div class="links-of-blogroll-title"><i class="fa fa-fw fa-link"></i> 推荐阅读</div><ul class="links-of-blogroll-list"><li class="links-of-blogroll-item"><a href="https://github.com/spring-projects" title="https:&#x2F;&#x2F;github.com&#x2F;spring-projects" rel="noopener" target="_blank">spring-projects</a></li><li class="links-of-blogroll-item"><a href="http://www.alloyteam.com/nav/" title="http:&#x2F;&#x2F;www.alloyteam.com&#x2F;nav&#x2F;" rel="noopener" target="_blank">Web前端导航</a></li><li class="links-of-blogroll-item"><a href="http://www.chuangzaoshi.com/code" title="http:&#x2F;&#x2F;www.chuangzaoshi.com&#x2F;code" rel="noopener" target="_blank">创造狮导航</a></li></ul></div></div></div></aside><div id="sidebar-dimmer"></div></div></main><footer class="footer"><div class="footer-inner"><div class="copyright">&copy; <span itemprop="copyrightYear">2020</span> <span class="with-love"><i class="fa fa-heart"></i> </span><span class="author" itemprop="copyrightHolder">宇宙小神特别萌</span> <span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-area-chart"></i> </span><span title="站点总字数">1.2m</span> <span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-coffee"></i> </span><span title="站点阅读时长">17:41</span></div><div class="busuanzi-count"><script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span class="post-meta-item" id="busuanzi_container_site_uv" style="display:none"><span class="post-meta-item-icon"><i class="fa fa-user"></i> </span><span class="site-uv" title="总访客量"><span id="busuanzi_value_site_uv"></span> </span></span><span class="post-meta-divider">|</span> <span class="post-meta-item" id="busuanzi_container_site_pv" style="display:none"><span class="post-meta-item-icon"><i class="fa fa-eye"></i> </span><span class="site-pv" title="总访问量"><span id="busuanzi_value_site_pv"></span></span></span></div></div></footer></div><script src="//cdn.jsdelivr.net/npm/animejs@3.1.0/lib/anime.min.js"></script><script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script><script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script><script src="//cdnjs.cloudflare.com/ajax/libs/velocity/1.2.1/velocity.min.js"></script><script src="//cdnjs.cloudflare.com/ajax/libs/velocity/1.2.1/velocity.ui.min.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script><script>!function(){var t=document.createElement("script"),e=window.location.protocol.split(":")[0];t.src="https"===e?"https://zz.bdstatic.com/linksubmit/push.js":"http://push.zhanzhang.baidu.com/push.js";var s=document.getElementsByTagName("script")[0];s.parentNode.insertBefore(t,s)}()</script><script src="/js/local-search.js"></script><script>NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//cdnjs.cloudflare.com/ajax/libs/valine/1.3.10/Valine.min.js', () => {
    <!--var GUEST = ['nick', 'mail', 'link'];-->
    var GUEST = ['nick', 'mail'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el: '#valine-comments',
      verify: false,
      notify: false,
      appId: 'OuyHeA0XGL8hK4vgbqL0iERx-gzGzoHsz',
      appKey: 'L58VglKmWULcGK2W2AN7kWzM',
      placeholder: "留下邮箱，有回复时你将收到提醒，邮箱不会被公开。",
      avatar: 'wavatar',
      meta: guest,
      pageSize: '10' || 10,
      visitor: true,
      lang: 'zh-cn' || 'zh-cn',
      path: location.pathname,
      recordIP: true,
      serverURLs: ''
    });
  }, window.Valine);
});</script><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({pluginRootPath:"live2dw/",pluginJsPath:"lib/",pluginModelPath:"assets/",tagMode:!1,model:{jsonPath:"/live2dw/assets/wanko.model.json"},display:{position:"right",width:150,height:300},mobile:{show:!1},log:!1})</script></body><script type="text/javascript" src="/js/src/activetitle.js"></script></html><!-- rebuild by neat -->